package com.daohang361.imqq.protocol;

import java.nio.charset.Charset;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import com.daohang361.imqq.Asycore.MyLoger;
import com.daohang361.imqq.Asycore.QQUser;
import com.daohang361.imqq.encoder.InterfaceEncoder;
import com.daohang361.imqq.encoder.LoginEncoder;
import com.daohang361.imqq.hotLoader.PuginManager;
import com.daohang361.imqq.ServerContex.ServerContex;
import com.daohang361.imqq.tool.Hex2Bytes;
import com.daohang361.imqq.tool.MyUtil;
/**
 * this class is used for encode the  HexString to bytes 
 * mina encode 编码控制
 */
public class HexEncodeProtocol extends ProtocolEncoderAdapter {
	private  Charset charset;
	public HexEncodeProtocol(Charset defaultCharset) {
		this.charset=defaultCharset;
	}
	public Charset getCharset() {
		return charset;
	}

	public void setCharset(Charset charset) {
		this.charset = charset;
	}

	@Override
	public void encode(IoSession session, Object object, ProtocolEncoderOutput out)
			throws Exception {
		QQUser myqq =null;
		try {
			myqq= (QQUser) object;
			myqq.setSendPacke();
			int logintimes= myqq.getLogintimes();
			byte[] sendByte =null;
			System.out.println("logintimes:"+logintimes+"发送第:"+myqq.getSendPacke()+"包");
			/**
			 * 最基本登陆,缺一不可
			 */
			if(logintimes<6){
				sendByte=new LoginEncoder().encode(myqq,0);
			}
			/**
			 * 登陆初始化模式开始,6 到 ServerContex.getSize()+5为固化补充完整登陆;此次后logintimes就是 命令的 int表达形式
			 */
		
			else if(logintimes>5 && logintimes<=ServerContex.getSize()+5){
				InterfaceEncoder encoder=ServerContex.getNow(logintimes-5-1);
				int command = ServerContex.getNowCommand(logintimes-5-1);
				System.out.println("command:"+command);
				sendByte=encoder.encode(myqq,command);
				if(encoder.getIgnore()&&ServerContex.getSize()+5==logintimes){
					myqq.setLogin(true);//如果是最后一步并且最后一步忽略接收情况则设置登陆成功,否则在decoder里面设置
					System.out.println("[encoder protocol]反馈登陆成功");
				}
			}
			else{
				if(myqq.getCommand()==0)
					return;
				else if(myqq.DirectSendLength()>0)
				{
					sendByte=myqq.TokeDirectSend();
				}else
				{
				InterfaceEncoder encoder=Myproper.getEncoder(myqq.getCommand());
				encoder=encoder==null?(InterfaceEncoder) PuginManager.GetPuginByKey("Encoder"+MyUtil.intToHex(myqq.getCommand())):encoder;
				if(encoder==null)
					System.out.println("尝试加载:"+MyUtil.intToHex(myqq.getCommand())+"失败!");
				sendByte=encoder.encode(myqq, myqq.getCommand());
				}
					
				System.out.println("HexEncodeProtocol command:"+myqq.getCommand());
			}
			
			if(sendByte==null)
				return;
			System.out.println("encode QQ["+myqq.getQq()+"] data to ip:"+session.getRemoteAddress());
			MyLoger.printByteToHex(Hex2Bytes.bytesToHexString(sendByte));
			IoBuffer buffer1 = IoBuffer.wrap(sendByte);
			out.write(buffer1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
